<template>
  <view>
    <u-popup :show="localShow" @close="closeGetyhq" mode="center" round="20" closeable
      customStyle="width:80%;overflow: hidden;">
      <view class="getyhq">
        <span v-if="couponinfo.deductionType==1" style="font-size: 60rpx;color: #fa3534;font-weight:900;">¥<text
            style="font-size: 120rpx;">{{ couponinfo.insteadPrice }}</text></span>
		<span v-if="couponinfo.deductionType==2" style="font-size: 60rpx;color: #fa3534;font-weight:900;"><text
		    style="font-size: 120rpx;">{{ couponinfo.insteadPrice/10 }}</text>折券</span>
        <p style="font-size: 32rpx;">{{ couponinfo.name }}</p>
        <p style="font-size: 32rpx;">
          {{ couponinfo.businessType === 1 ? 'vip 充值可用' : couponinfo.businessType === 2 ? '增量包充值可用' : '全场通用优惠券' }}
        </p>
        <p style="font-size: 28rpx;color: #666666;">每人限领 1 张</p>
        <p style="font-size: 28rpx;color: #999999;">有效期：至{{ couponinfo.expireTime }}</p>
        <view style="width: 70%;">
          <u-button color="#fa3534" shape="circle" size="large" text="立即领取" @click="getyhq"></u-button>
        </view>
      </view>
    </u-popup>
  </view>
</template>

<script>
import Index from "@/common/index-model.js"
let index = new Index();
let that = null;

export default {
  name: "CouponPopup",
  props: {
    // 控制弹窗显示的参数
    showGetyhq: {
      type: Boolean,
      default: false
    },
    // 优惠券 ID
    couponId: {
      type: String,
      default: ''
    },
	businessId: {
	  type: String,
	  default: ''
	}
  },
  data() {
    return {
      localShow: this.showGetyhq,
      couponinfo: {}, // 初始化为对象
      buttonlock: false,
      isFetching: false // 防重复请求
    };
  },
   watch: {
      showGetyhq: {
        immediate: true, // 立即执行一次监听器
        handler(newValue) {
          this.localShow = newValue;
          if (newValue && !this.isFetching) {
            this.getCouponinfo();
          }
        }
      },
    },
  methods: {
    closeGetyhq() {
      this.localShow = false;
      this.$emit('update:showGetyhq', this.localShow);
    },
    getyhq() {
      if (this.buttonlock) {
        return;
      }
      this.buttonlock = true;
      console.log('领取优惠券');
      index.getPublicUserMember({activityId:this.couponId}, `/apclient/appActivity/receive`, 'post', res => {
        if (res.data.code === 200 && res.data.success) {
          console.log("领取成功", res.data);
          index.show_tips("领取成功");
		  this.$emit('onReceiveSuccess')
          setTimeout(() => {
            this.buttonlock = false;
            this.localShow = false;
            this.$emit('update:showGetyhq', this.localShow);
          }, 1000);
        } else {
          index.show_tips(res.data.message);
          this.buttonlock = false;
        }
      });
    },
    getCouponinfo() {
      if (this.isFetching) return; // 防止重复请求
      this.isFetching = true;

      index.getPublicUserMember({}, `/apclient/appCouponInfo/detail/${this.businessId}`, 'get', res => {
        this.isFetching = false;
        if (res.data.code === 200 && res.data.success) {
          this.couponinfo = res.data.result;
          console.log("这是优惠券",this.couponinfo);
        } else {
          index.show_tips(res.data.message);
          this.closeGetyhq(); // 获取失败，关闭弹窗
        }
      });
    },
  }
}
</script>

<style lang="scss">
.getyhq {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 40rpx 0;

  p {
    padding: 8rpx 0;
  }
}
</style>